﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace SilkeyERP.UI.Entities.Shared
{

    //// Special EventArgs class to hold info about Shapes. 
    //public class BaseChooserOnLoadEventArgs : EventArgs
    //{
    //    private string _baseChooserTitle;

    //    public BaseChooserOnLoadEventArgs(string baseChooserTitle)
    //    {
    //        _baseChooserTitle = baseChooserTitle;
    //    }
    //    public string BaseChooserTitle
    //    {
    //        get { return _baseChooserTitle; }
    //    }
    //}


    public partial class BaseChooser : DevExpress.XtraEditors.XtraForm
    {
        // The event. Note that by using the generic EventHandler<T> event type 
        // we do not need to declare a separate delegate type. 
        //public event EventHandler<BaseChooserOnLoadEventArgs> BaseChooserLoad;

        public List<dynamic> SelectedFields { get; private set; }

        public BaseChooser()
        {
            InitializeComponent();
        }

        private void BaseChooser_Load(object sender, EventArgs e)
        {
            OnLoad();

            if (SelectedFields == null)
                SelectedFields = new List<dynamic>();
            else
                SelectedFields.Clear();
        }

        protected virtual void OnLoad()
        {            
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SelectionConfirm();
        }

        private void SelectionConfirm()
        {
            foreach (var rowIndex in gvBaseChooser.GetSelectedRows())
                SelectedFields.Add(bsBaseChooser.List[rowIndex]);

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            SelectedFields.Clear();
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void gvBaseChooser_DoubleClick(object sender, EventArgs e)
        {
            GridView view = (GridView)sender;

            Point pt = view.GridControl.PointToClient(Control.MousePosition);

            DoRowDoubleClick(view, pt);

        }


        private void DoRowDoubleClick(GridView view, Point pt)
        {
            GridHitInfo info = view.CalcHitInfo(pt);

            if (info.InRow || info.InRowCell)            
                SelectionConfirm();                      
        }
    }
}